package com.baidu.service.impl;

import com.baidu.mapper.UserMapper;
import com.baidu.pojo.Result;
import com.baidu.pojo.User;
import com.baidu.service.UserService;
import com.baidu.util.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author ZYH
 * @version 1.0
 * @description:TODO
 * @date 2021/12/30 9:07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public Result sendCode(String phone) {
        rabbitTemplate.convertAndSend("demoyk1223sendchange","demoyk1223sendqueue",phone);
        return new Result(200,"success",true);
    }

    @Override
    public Result zhuce(User user) {
        String s = stringRedisTemplate.opsForValue().get(user.getPhone());
        if (StringUtils.isBlank(s)){
            return new Result(201,"验证码过期",false);
        }
        if(!s.equals(user.getCode())){
            return new Result(202,"验证码错误",false);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getPhone,user.getPhone());

        List<User> list = userMapper.selectList(queryWrapper);
        if(list.size()>0){
            return new Result(203,"手机号为空",false);
        }
        userMapper.insert(user);
        return new Result(200,"注册成功",true);
    }

    @Override
    public Result login(User user) {
        String s = stringRedisTemplate.opsForValue().get(user.getUuid());
        if(StringUtils.isBlank(s)){
            return new Result(201,"验证码过期",false);
        }
        if(!s.equalsIgnoreCase(user.getCode())){
            return new Result(202,"验证码错误",false);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getPhone,user.getPhone());
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1==null){
            return new Result(203,"用户名不存在",false);
        }
        if(user.getPassword().equals(user1.getPassword())){
            String token = JwtUtils.createToken(user1.getUid() + "", user1.getPhone(), "", null);
            stringRedisTemplate.opsForValue().set("token", token,30,TimeUnit.MINUTES);
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("token",token);
            hashMap.put("user",user1);
            System.out.println(token+"===========");
            return new Result(200,hashMap,true);
        }else {
            return new Result(205,"密码错误",false);
        }

    }
}
